package cc.fs.kotlin.ui.activity.helper

import android.app.ProgressDialog
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import cc.fs.kotlin.ui.activity.StartActivity
import org.jetbrains.anko.intentFor
import java.lang.ref.WeakReference
import kotlin.collections.arrayListOf

abstract class BaseActivity : AppCompatActivity() {
    companion object {
        private val MSG_FINISH = 121915
        private val handlers = arrayListOf<MyHandler>()

        fun addHandler(handler:MyHandler) {
            handlers.add(handler)
        }

        fun deleteHandler(handler:Companion.MyHandler) {
            handlers.remove(handler)
        }

        fun removeAll() {
            handlers.clear()
        }

        /** 关闭应用所有页面 */
        fun notiFinishAllActivity(){
            val msg = Message()
            msg.what = MSG_FINISH
            broadcast(msg)
        }

        fun broadcast(message: Message) {
            Log.e("--出现错误-","-----------22222---------------------")
            for (handler in Companion.handlers) {
                Log.e("--出现错误-","-------------3333333333-------------------")
                val msg = Message()
                msg.copyFrom(message)
                handler.sendMessage(msg)
            }
        }

        class MyHandler : Handler {
            private val mOuter: WeakReference<BaseActivity>

            constructor(activity:BaseActivity) {
                mOuter = WeakReference(activity)
            }

            override fun dispatchMessage(msg: Message?) {
                super.dispatchMessage(msg)
                Log.e("--出现错误-","-------------41414141411414-------------------")

            }

            override fun handleMessage(msg: Message) {
                if (mOuter.get() == null) {
                    deleteHandler(this)
                    return
                } else {
                    Log.e("--出现错误-","-------------44444444444-------------------")

                    if (msg.what == -101 && msg.arg1 == -102 && msg.arg2 == -103) {
                        val runnable = msg.obj as () -> Unit
                        runnable.invoke()
                        return
                    }

                    /** 结束所有activity */
                    if(msg.what == BaseActivity.Companion.MSG_FINISH){
                        mOuter.get().finish()
                        return
                    }

                    mOuter.get().handler(msg)
                }
            }
        }
    }

    private var isInForeground = false

    protected val handler: MyHandler by lazy {
        MyHandler(this)
    }

    fun sendMsg(message: Message) {
        val msg = Message()
        msg.copyFrom(message)
        handler.sendMessage(msg)
    }

    fun sendMsgDelayed(message: Message, delayMillis: Int) {
        val msg = Message()
        msg.copyFrom(message)
        handler.sendMessageDelayed(msg, delayMillis.toLong())
    }

    fun runDelayed(runnable: () -> Unit, delayMillis: Int) {
        val msg = Message()
        msg.what = -101
        msg.arg1 = -102
        msg.arg2 = -103
        msg.obj = runnable
        handler.sendMessageDelayed(msg, delayMillis.toLong())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addHandler(handler)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> this.finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onResume() {
        super.onResume()
        isInForeground = true
    }

    override fun onPause() {
        super.onPause()
        isInForeground = false
    }

    override fun finish() {
        deleteHandler(handler)
        super.finish()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    /** 显示正在加载dialog */
    var waitDialog: ProgressDialog ?= null
    fun showWaitDialog(){
        if(waitDialog == null){
            waitDialog = ProgressDialog(this@BaseActivity)
            waitDialog?.setMessage("正在加载...")
            waitDialog?.show()
        } else {
            waitDialog?.show()
        }
    }

    fun hidenWaitDialog(){
        if(waitDialog != null){
            waitDialog?.dismiss()
        }
    }

    fun showToast(msg:String){
        Toast.makeText(this@BaseActivity,msg,Toast.LENGTH_SHORT).show()
    }

    abstract fun handler(msg: Message)

}
